/**
 * 
 */
package com.gr.staffpm.feedback.service;

import java.util.List;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gr.staffpm.datatypes.Feedback;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.feedback.dao.FeedbackDAO;

/**
 * Default implementation of the {@link FeedbackService} interface.  This service implements
 * operations related to User Feedback data.
 * 
 * @author Graham Rhodes 3 Mar 2011 12:20:26
 */
@Transactional
@Service("feedbackService")
public class DefaultFeedbackService implements FeedbackService {

    private FeedbackDAO feedbackDao;

    @Autowired
    public void setFeedbackDAO(FeedbackDAO feedbackDao) {
        this.feedbackDao = feedbackDao;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Feedback> getFeedbackForUserBetweenDatesWithRating(User user, DateTime start, DateTime end, int rating) {
        return feedbackDao.getFeedbackForUserBetweenDatesWithRating(user, start, end, rating);
    }

    @Override
    public void addFeedback(String comment, int rating, User reciever, User creator) {
        feedbackDao.addOrUpdateFeedback(new Feedback(rating, comment, creator, reciever));
    }

    @Override
    @Transactional(readOnly = true)
    public List<Feedback> getFeedbackForUserBetweenDates(User user, DateTime start, DateTime end) {
        return feedbackDao.getFeedbackForUserBetweenDates(user, start.toDate(), end.toDate());
    }

    @Override
    @Transactional(readOnly = true)
    public int countRatingForDay(User user, DateTime date, int rating) {
        return feedbackDao.countRatingBetween(user, date.withTime(0, 0, 0, 0), date.withTime(date.hourOfDay().getMaximumValue(), 59, 59, 59), rating);
    }

}
